package com.base.self.es.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.base.log.WebLog;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.entity.Commodity;
import com.base.self.es.model.CommodityEs;
import com.base.self.es.repository.CommodityEsRepository;
import com.base.self.mapper.CommodityMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/test/selfOrderComment")
@Api(tags = "ES测试")
public class EsTestController {
    private final CommodityEsRepository commodityEsRepository;

    private final CommodityMapper commodityMapper;

    public EsTestController(CommodityEsRepository commodityEsRepository,
                            CommodityMapper commodityMapper) {
        this.commodityEsRepository = commodityEsRepository;
        this.commodityMapper = commodityMapper;
    }

    @PostMapping(value = "/transitionList")
    @ApiOperation(value="ES测试分页查询")
    @WebLog
    public ResponseResult transitionList() {
        Page<CommodityEs> page = commodityEsRepository.search(null, PageRequest.of(0, 10));
        return ResponseResultUtils.getResponseResultS("查询成功",page.getContent());
    }

    @PostMapping(value = "/transition")
    @ApiOperation(value="ES测试更新商品信息")
    @WebLog
    public ResponseResult transition() {
        List<Commodity> list = commodityMapper.selectList(new LambdaQueryWrapper<Commodity>().eq(Commodity::getIsDel, 0));
        commodityEsRepository.deleteAll();
        list.forEach(x -> {
            final CommodityEs commodityEs = CommodityEs
                    .builder()
                    .id(x.getId())
                    .commodityMarketPrice(x.getCommodityMarketPrice())
                    .commodityName(x.getCommodityName())
                    .commodityRealPrice(x.getCommodityRealPrice())
                    .commoditySales(x.getCommoditySales())
                    .commodityShowPic(x.getCommodityShowPic())
                    .commodityClassifyLevel1(x.getCommodityClassifyLevel1())
                    .commodityClassifyLevel2(x.getCommodityClassifyLevel2())
                    .commoditySubarea(x.getCommoditySubarea())
                    .createTime(x.getCreateTime())
                    .build();
            commodityEsRepository.save(commodityEs);
        });
        return ResponseResultUtils.getResponseResultS("添加成功");
    }

    @PostMapping(value = "/search")
    @ApiOperation(value="ES测试搜索商品信息")
    @WebLog
    public ResponseResult transition(@ApiParam @RequestParam String commodityName) {
        QueryBuilder customerQuery = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("commodityName", commodityName));
        Page<CommodityEs> page = commodityEsRepository.search(customerQuery, PageRequest.of(0, 10));
        return ResponseResultUtils.getResponseResultS("查询成功",page.getContent());
    }
}
